// Copyright (C) 2018 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-atomics.exchange
description: >
  Test range checking of Atomics.exchange on arrays that allow atomic operations
includes: [testAtomics.js, testBigIntTypedArray.js]
features: [ArrayBuffer, Atomics, BigInt, DataView, SharedArrayBuffer, Symbol, TypedArray]
---*/
function Test() {
    var buffer = new SharedArrayBuffer(BigInt64Array.BYTES_PER_ELEMENT * 2);

    testWithBigIntTypedArrayConstructors(function(TA) {
      let view = new TA(buffer);

      testWithAtomicsOutOfBoundsIndices(function(IdxGen) {
        assert.throws(RangeError, function() {
          Atomics.exchange(view, IdxGen(view), 10n, 0n);
        }, '`Atomics.exchange(view, IdxGen(view), 10n, 0n)` throws RangeError');
      });
    });
}

// preheat
const NUMBER_OF_PREHEAT = 1;
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}

ArkTools.jitCompileAsync(Test);
assert.sameValue(ArkTools.waitJitCompileFinish(Test), true, '[JIT] JIT disabled.');

// test
const NUMBER_OF_TEST = 1; // or 2
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}
